package com.mryx.demo.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import com.mryx.demo.pojo.*;
import com.mryx.demo.service.serviceImpl.OrderServiceImpl;

import com.sun.org.apache.xpath.internal.operations.Or;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 订单接口
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    @ApiOperation(value = "生成订单")
    @RequestMapping(value = "/Insert",method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200,message = "操作成功"),@ApiResponse(code = 400,message = "操作失败")})
    @ApiImplicitParams({
    })
    public String InOrder(List<Cart> carts){
        //获取用户ID
        int userid= Integer.parseInt(redisTemplate.opsForValue().get("uid"));
        String orderwzf= redisTemplate.opsForValue().get(userid+"DD");
        if(orderwzf!=null){
            return "400:用户存在未支付订单";
        }
        Double Allmoney=0.0;


        //生成随机订单号
        Date date=new Date();
        String ordernum="DD"+date.getTime();

        List<Details> detailss=new ArrayList<>();
        //订单详情添加
        for (int i = 0; i <carts.size() ; i++) {
            Details details=new Details();
            details.setCid(carts.get(i).getCid());
            details.setNumber(carts.get(i).getComnum());
            details.setOrdernum(ordernum);
            //获取单价求总价
            Double danjia=orderService.ComPrice(carts.get(i).getCid());
            Double zong=danjia*carts.get(i).getComnum();
            details.setPrice(danjia);
            details.setPrices(zong);
            Allmoney=Allmoney+zong;
            //添加
            //   orderService.InDetas(details);
            detailss.add(details);
        }
        //详情集合添加到redis
        Gson gson=new Gson();
        redisTemplate.opsForValue().set(userid+"DDXQ",gson.toJson(detailss),30,TimeUnit.MINUTES);

        //订单添加
        Order order=new Order();
        order.setOrdernum(ordernum);
        order.setPrices(Allmoney);
        order.setState(1);
        order.setUid(userid);
     //   orderService.InOrder(order);
        //订单添加到redis
        redisTemplate.opsForValue().set(userid+"DD",gson.toJson(order),30,TimeUnit.MINUTES);

        return "200";
    }

    @ApiOperation(value = "查看个人订单(全部)")
    @RequestMapping(value = "/showAll",method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200,message = "操作成功"),@ApiResponse(code = 400,message = "操作失败")})
    public List<Order> showAll(){
        //所有订单展示
        int uid= Integer.parseInt(redisTemplate.opsForValue().get("uid"));
        List<Order>  orders=new ArrayList<>();


        //查看redis中未付款订单
        String orderwzf= redisTemplate.opsForValue().get(uid+"DD");
        if(orderwzf!=null){
        //将json字符串转化为实体对象
        Gson gson=new Gson();
        Order listwzf=gson.fromJson(orderwzf,Order.class);
        //查看未支付的商品图片
        //声明图片集合
        List<Picture> pictures= new ArrayList<>();
        //订单详情列表
        String ddxq= redisTemplate.opsForValue().get(uid+"DDXQ");
        List<Details> detailss=gson.fromJson(ddxq,new TypeToken<List<Order>>(){}.getType());

        for(int i=0;i<detailss.size();i++){
            Picture picture=new Picture();
            //获取商品id
           int cid= detailss.get(i).getCid();
           //获取商品图片
           String route= orderService.Compicture(cid);
           picture.setCid(cid);
           picture.setRoute(route);
           //添加至订单中图片集合
           pictures.add(picture);
        }
        listwzf.setPicture(pictures);
       //将未支付订单添加至总订单
        orders.add(listwzf);
        }

        //查看SQL中所有订单
       List<Order> listorder= orderService.showAll(uid);
        //查看订单中商品图片
        for (int i = 0; i <listorder.size() ; i++) {
          String ordernum= listorder.get(i).getOrdernum();
          List<Picture> pictureList= orderService.picture(ordernum);
          listorder.get(i).setPicture(pictureList);
            //将支付订单添加至总订单
            orders.add(listorder.get(i));
        }

        return orders;
    }

    @ApiOperation(value = "查看个人订单(类别)")
    @RequestMapping(value = "/showByClass",method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200,message = "操作成功"),@ApiResponse(code = 400,message = "操作失败"),@ApiResponse(code = 1,message = "未付款"),@ApiResponse(code = 2,message = "待发货"),@ApiResponse(code = 3,message = "已收货"),})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state",value = "订单状态",required = true,dataType = "int")
    })
    public List<Order> showByClass(int state){
        int uid= Integer.parseInt(redisTemplate.opsForValue().get("uid"));
        if(state==1){
            //查看redis中未付款订单
            String orderwzf= redisTemplate.opsForValue().get(uid+"DD");
            //将json字符串转化为实体对象
            Gson gson=new Gson();
            Order listwzf=gson.fromJson(orderwzf,Order.class);
            //查看未支付的商品图片

            //声明图片集合
            List<Picture> pictures= new ArrayList<>();
            //订单详情列表
            String ddxq= redisTemplate.opsForValue().get(uid+"DDXQ");
            List<Details> detailss=gson.fromJson(ddxq,new TypeToken<List<Order>>(){}.getType());
            for(int i=0;i<detailss.size();i++){
                Picture picture=new Picture();
                //获取商品id
                int cid= detailss.get(i).getCid();
                //获取商品图片
                String route= orderService.Compicture(cid);
                picture.setCid(cid);
                picture.setRoute(route);
                //添加至订单中图片集合
                pictures.add(picture);
            }
            listwzf.setPicture(pictures);
            //对象转化集合(返回值类型)
            List<Order> orders=new ArrayList<>();
            orders.add(listwzf);
            return orders;
        }
        //查看所有订单
        List<Order> listorder= orderService.showByClass(uid,state);
        //查看订单中商品图片
        for (int i = 0; i <listorder.size() ; i++) {
            String ordernum= listorder.get(i).getOrdernum();
            List<Picture> pictureList= orderService.picture(ordernum);
            listorder.get(i).setPicture(pictureList);
        }
        return listorder;
    }

    @ApiOperation(value = "修改订单状态")
    @RequestMapping(value = "/Update",method = RequestMethod.GET)
    @ApiResponses({@ApiResponse(code = 200,message = "操作成功"),
            @ApiResponse(code = 400,message = "操作失败"),
            @ApiResponse(code = 1,message = "未付款"),
            @ApiResponse(code = 2,message = "待发货"),
            @ApiResponse(code = 3,message = "已收货"),})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordernum",value = "订单编号",
                    required = true,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "订单状态",
                    required = true,dataType = "int")
    })
    public int Update(String ordernum,int state){
        Order order=new Order();
        order.setState(state);
        order.setOrdernum(ordernum);
        orderService.Update(order);
        return 200;
    }


}
